package com.deer.wms.ware.task.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.system.manage.model.user.UserInfo;
import com.deer.wms.system.manage.service.UserInfoService;
import com.deer.wms.ware.task.constant.WareTaskConstant;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.Wave.*;
import com.deer.wms.ware.task.service.WaveDetailService;
import com.deer.wms.ware.task.service.WaveMasterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
*
 * @author wei
 * @date 2020/02/02
 */
@RestController
@RequestMapping("/wave/details")
public class WaveDetailController {

    @Autowired
    private WaveDetailService waveDetailService;
    @Autowired
    private WaveMasterService waveMasterService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private InventoryService inventoryService;

    @PostMapping
    public Result add(@RequestBody WaveDetail waveDetail) {
        waveDetailService.save(waveDetail);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/addFromSo")
    public Result addFromSo(@RequestBody WaveDetailInsertData insertData) {
        WaveMaster wave = waveMasterService.getById(insertData.getWaveMasterId());
        for (Integer soId : insertData.getSoMasterIds()) {
            List<WaveDetailDto> waveDetail1 = waveDetailService.findList(new WaveDetailCriteria(soId));
            if(waveDetail1.size()>0){
                return ResultGenerator.genFailResult(CommonCode.SERVER_INERNAL_ERROR,"选中工单中有已添加批次",null);
            }
            WaveDetail waveDetail = new WaveDetail();
            waveDetail.setWaveMasterId(wave.getWaveId());
//            waveDetail.setSoMasterId(soId);
            waveDetailService.save(waveDetail);
        }
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        waveDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody WaveDetail waveDetail) {
        waveDetailService.updateById(waveDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        List<WaveDetail> waveDetails = waveDetailService.getByMasterId(id);
        return ResultGenerator.genSuccessResult(waveDetails);
    }

    @GetMapping
    public Result list(WaveDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<WaveDetailDto> list = waveDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/list/pick")
    public Result listForPick(WaveDetailCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        QueryWrapper<WaveDetail> qw = new QueryWrapper<>();
        qw.eq("ware_id", criteria.getWareId());
        qw.eq("pick_user_id", currentUser.getUserId());
        qw.in("state", 1, 2);
        List<WaveDetail> detailList = waveDetailService.list(qw);
        List<WaveDetail> collect = detailList.stream().filter(detail -> detail.getQuantity() > detail.getPickQuantity()).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo(collect);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/suggest")
    public Result suggestForPick(@RequestBody WaveDetailCriteria waveDetailCriteria, @ApiIgnore @User CurrentUser currentUser) {
        UserInfo wareUser = userInfoService.getById(waveDetailCriteria.getUserId());
        List<String> idsList = Arrays.asList(waveDetailCriteria.getIds().split(","));
        List<WaveDetail> waveDetailList = waveDetailService.listByIds(idsList);
        waveDetailService.suggestForPick(waveDetailList, wareUser, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/sendOut")
    public Result sendOut(String pickIds, Integer wareId, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER);
        }

        String[] split = pickIds.split(",");
        List<Integer> ids = new ArrayList<>();
        for (String s : split) {
            ids.add(Integer.valueOf(s));
        }

        waveDetailService.sendOut(ids, currentUser);

        //出库单据回传erp
//        soMasterService.outbillToErpNew(billNo);
        return ResultGenerator.genSuccessResult();
    }

}
